#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

vector<int> advantageCount(vector<int>& nums1, vector<int>& nums2) {
    int n = nums1.size();
    sort(nums1.begin(), nums1.end());
    vector<int> index2(n);
    for (int i = 0; i < n; i++) index2[i] = i;
    sort(index2.begin(), index2.end(), [&](int i, int j) {return nums2[i] < nums2[j]; });

    vector<int>ret(n);
    int left = 0, right = n - 1;
    for (auto a : nums1)
    {
        if (a > nums2[index2[left]]) ret[index2[left++]] = a;
        else ret[index2[right--]] = a;
    }
    return ret;
}


int main()
{
    vector<int> nums1 = { 2,7,11,15 };
    vector<int> nums2 = { 1,10,4,11 };
    auto ret = advantageCount(nums1, nums2);
    for (auto a : ret)
    {
        cout << a << " ";
    }
    cout << endl;
    return 0;
}